package com.prime.tutorial.moviestore.view.techsupport;

import com.prime.tutorial.moviestore.domain.Device;
import com.prime.tutorial.moviestore.service.techsupport.TechSupportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.faces.component.html.HtmlDataTable;
import javax.faces.event.ActionEvent;
import java.io.Serializable;

/**
 * Responsible for techSupport device removal.
 * Provides the dataTable for the binding with the list of devices from ListDevicesForRemoval and
 * provides the link to the service layer of techSupport.
 * @author vasylja
 */

@Component("removeDevice")
@Scope("request")
public class RemoveDevice implements Serializable {
    private TechSupportService techSupportService;
    private Device[] arrayDeviceToRemove;

    private HtmlDataTable dataTable;

    @Autowired
    public RemoveDevice(TechSupportService techSupportService) {
        setTechSupportService(techSupportService);
    }

    public HtmlDataTable getDataTable() {
        return dataTable;
    }

    public void setDataTable(HtmlDataTable dataTable) {
        this.dataTable = dataTable;
    }

    private TechSupportService getTechSupportService() {
        return techSupportService;
    }

    private void setTechSupportService(TechSupportService techSupportService) {
        this.techSupportService = techSupportService;
    }
    public String removeDevice(){
        techSupportService.removeDevice((Device) dataTable.getRowData());
        return "removeDevice";
    }

    public void removeDevice(ActionEvent e){
        for(int i=0;i<getArrayDeviceToRemove().length;i++){
            techSupportService.removeDevice(getArrayDeviceToRemove()[i]);
        }
        setArrayDeviceToRemove(null);
    }

    public Device[] getArrayDeviceToRemove() {
        return arrayDeviceToRemove;
    }

    public void setArrayDeviceToRemove(Device[] arrayDeviceToRemove) {
        this.arrayDeviceToRemove = arrayDeviceToRemove;
    }
}
